/**
 * 
 */
package com.ccqlk.attendance.controller.manage.attendance;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ccqlk.attendance.common.page.PageRequest;
import com.ccqlk.attendance.controller.manage.AbstractManageController;
import com.ccqlk.attendance.model.AttendanceLog;
import com.ccqlk.attendance.model.Staff;
import com.ccqlk.attendance.repository.service.AttendanceLogService;
import com.ccqlk.attendance.repository.service.StaffService;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * Copyright 2015-2018 重庆鲁控科技有限公司<br>
 * 文件标题: AttendanceLogManageController<br>
 * 文件说明: 出勤记录查看管理器<br>
 * @author ZhangYinxian 348893009@qq.com<br>
 * 创建时间 2016年5月27日 下午3:35:06<br>
 */
@Controller
@RequestMapping("manage/attendance/log")
public class AttendanceLogManageController extends AbstractManageController<AttendanceLog, Integer> {

	private AttendanceLogService attendanceLogService;
	
	private @Resource StaffService staffService;
	
	@Autowired
	public AttendanceLogManageController(AttendanceLogService attendanceLogService) {
		super(attendanceLogService, "manage/attendance/log/", "attendanceLog");
		this.attendanceLogService = attendanceLogService;
	}

	/* (non-Javadoc)
	 * @see com.ccqlk.attendance.controller.manage.AbstractManageController#getData(com.ccqlk.attendance.common.page.PageRequest, javax.servlet.http.HttpServletRequest, org.springframework.ui.Model, boolean)
	 */
	@Override
	public PageInfo<AttendanceLog> getData(PageRequest pageRequest, HttpServletRequest request, Model model, boolean pageable) {
		PageInfo<AttendanceLog> pageInfo = attendanceLogService.selectAll(pageRequest);
		fillStaffs(pageInfo);
		return pageInfo;
	}

	/**
	 * @param pageInfo
	 */
	private void fillStaffs(PageInfo<AttendanceLog> pageInfo) {
		if (pageInfo==null) {
			return;
		}

		List<AttendanceLog> attendanceLogs = pageInfo.getList();
        if (attendanceLogs==null || attendanceLogs.isEmpty()) {
			return;
		}
        
        List<String> staffCodeList = Lists.newArrayList();
        for (AttendanceLog attendanceLog : attendanceLogs) {
			staffCodeList.add(attendanceLog.getStaffCode());
		}
		
        if (staffCodeList==null || staffCodeList.isEmpty()) {
			return;
		}
        
        Map<String, Object> params = Maps.newHashMap();
        params.put("codeList", staffCodeList);
        List<Staff> staffs = staffService.selectByParams(params);
        if (staffs==null || staffs.isEmpty()) {
			return;
		}
        
        Map<String, Staff> staffMap = Maps.newHashMap();
        for (Staff staff : staffs) {
			staffMap.put(staff.getCode(), staff);
		}
        
        for (AttendanceLog attendanceLog : attendanceLogs) {
			attendanceLog.setStaff(staffMap.get(attendanceLog.getStaffCode()));
		}
		
	}

}
